<template>
  <BasicLayout>
    <template #wrapper>
      <el-card class="box-card">
        <el-row :gutter="10" class="mb10" style="float:right">
          <el-col :span="1.5">
            <el-upload :headers="headers" accept=".pdf" :show-file-list="false" :before-upload="beforeUpload"
              :http-request="httpRequest" action="http://47.109.17.77:8000/api/v1/public/uploadEsignTemplate">
              <el-button v-loading="uploadLoading" type="primary">合同模板上传</el-button>
            </el-upload>
          </el-col>
        </el-row>

        <el-table v-loading="loading" :data="courseList" border>
          <el-table-column label="编码" sortable="custom" width="75" type="index" />
          <el-table-column label="模板名称" prop="file_name" :show-overflow-tooltip="true" />
          <el-table-column label="创建时间" sortable="custom" prop="createdAt" width="160">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createdAt) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" class-name="small-padding fixed-width" width="140">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-view" @click="handleDetail(scope.row)">详情
              </el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageIndex"
          :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改对话框 -->
        <el-drawer ref="drawer" :title="title" :visible.sync="open" direction="rtl" custom-class="demo-drawer">
          <div class="demo-drawer__content">
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
              <el-form-item label="名称" prop="name">
                <el-input v-model="form.name" placeholder="请输入" />
              </el-form-item>
            </el-form>
            <div class="demo-drawer__footer">
              <el-button type="primary" @click="submitForm">确 定</el-button>
              <el-button @click="cancel">取 消</el-button>
            </div>
          </div>
        </el-drawer>
      </el-card>
    </template>
  </BasicLayout>
</template>

<script>
import { gitLocList, addContract, getTemplateDetail, deleteESign } from '@/api/admin/sys-consult'
import { getToken } from '@/utils/auth'
export default {
  name: 'SysAcademy',
  data() {
    return {
      headers: { Authorization: 'Bearer ' + getToken() },
      // 遮罩层
      loading: true,
      uploadLoading: false,
      // 选中数组
      chooseIds: [],
      // 总条数
      total: 0,
      // 表格数据
      courseList: [],
      // 排序字段
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      form: {},
      rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        province: [{ required: true, message: '选择适用地区', trigger: 'blur' }]
      },
      flieList: []
    }
  },

  created() {
    this.getList()
  },
  methods: {
    /** 查询参数列表 */
    getList() {
      this.loading = true
      gitLocList(this.queryParams).then(
        response => {
          const { data } = response
          this.courseList = data?.list
          this.total = data?.count
          this.loading = false
        }
      )
    },
    httpRequest(files) {
      const fd = new FormData()
      fd.append('file', files.file)
      addContract(fd).then(
        response => {
          const { data } = response
          window.location.href = data?.data?.docTemplateCreateUrl
          this.uploadLoading = false
        }
      )
    },
    beforeUpload() {
      this.uploadLoading = true
    },
    // 表单重置
    reset() {
      this.form = {}
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    /** 提交 */
    submitForm() {

    },
    // /** 编辑 */
    handleEdit(row) {

    },
    // /** 详情 */
    handleDetail(row) {
      getTemplateDetail({
        docTemplateId: row?.template_id
      }).then(res => {
        const { data } = res
        window.location.href = data?.fileDownloadUrl
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id // (row.template_id && [row.template_id])
      this.$confirm('是否确认删除', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(function () {
          return deleteESign(id)
        })
        .then(response => {
          if (response.code === 200) {
            this.msgSuccess(response.msg)
            this.getList()
          } else {
            this.msgError(response.msg)
          }
        })
        .catch(function () { })
    },
  }
}
</script>

<style>

</style>
